﻿/// <reference path="JQuery/jquery-1.4.4.min.vsdoc.js" />
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  Copyright(c)    2011    @kaleyroy   kaleyroy@gmail.com
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/***********************
//  加载级别信息
************************/
function loadLevelOptions() {
    $.ajax({
        type: "post",
        url: "../Level/EnabledList",
        dataType: "json",
        success: function (data) {
            var optionsHtml = "";
            $("#level-options").html("");
            optionsHtml += "<div style='background:#fafafa;padding:2px;'>选择级别</div>";
            for (var i = 0; i < data.length; i++) {
                optionsHtml += "<label><span style='margin-right:5px;background:" + data[i].color + "'>&nbsp;&nbsp;</span><input type='radio' color='" + data[i].color + "' name='slevel' value='" + data[i].id + "'><span>" + data[i].text + "</span></label>";
                if (i < data.length) {
                    optionsHtml += "<br>";
                }
            }
            $("#level-options").html(optionsHtml);
            //$("#level-options").resizable();

            //会员级别
            $("#levelid").combo({
                required: true,
                editable: false,
                missingMessage: "会员级别为必选项!",
                onChange: function (newValue, oldValue) {
                    if (newValue == 1) {
                        $("#regmoney").val(1000);
                    } else if (newValue == 2) {
                        $("#regmoney").val(2000);
                    }
                    $("#regmoney").numberbox("fix");
                }
            });
            $("#level-options").appendTo($("#levelid").combo("panel"));
            $("#level-options input").click(function () {
                var v = $(this).val();
                var s = $(this).next('span').text();
                $("#levelid").combo("setValue", v).combo("setText", s).combo("hidePanel");
                $("#level-color").css("background", ""+$(this).attr("color")+"");
            });

            //Testing
            //$("#level-select").combo("setValue", 2);
            //$("#level-select").combo("setText", "二级会员");
        }
    });
}

/** Validatebox验证扩展 **/
$.extend($.fn.validatebox.defaults.rules, {
    account: {
        validator: function (value, param) {
            return /^\w{3,16}$/.test(value);
        },
        message: '会员帐号必须为字母、数字及下划线,3-16位字符.'
    },
    password: {
        validator: function (value, param) {
            return /^\S{3,8}$/.test(value);
        },
        message: "密码必须为3-8为非空格字符."
    },
    equal: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: "两次密码字符不一致."
    },
    bankcard: {
        validator: function (value, param) {
            return /^\d{16}|\d{19}$/.test(value);
        },
        message: "银行卡号应为16或19为数字."
    },
    idcard: {
        validator: function (value, param) {
            return /^\d{15}|\d{18}$/.test(value);
        },
        message: "身份证号码应为15或16为数字."
    },
    phone: {
        validator: function (value, param) {
            return /^(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})$/.test(value);
        },
        message: "应为电话号码(0771-530000 | 530000)或者手机号码(13500000000)."
    }
});

/***********************
//  文档加载
************************/
$(function () {

    //$("#register-window").window("close");

    /***********************
    //  初始化工作
    ************************/
    //加载级别信息
    loadLevelOptions();

    //会员编号
    $("#usercode").validatebox({
        required: true,
        validType: "account",
        missingMessage: "会员编号为必填项!"
    });
    //Ajax验证会员编号
    $("#usercode").blur(function () {
        var value = $("#usercode").val();
        if (jQuery.trim(value).length < 3) {
            $("#usercode-message").html("");
            return;
        }
        $.ajax({
            type: "post",
            url: "../../controller.aspx?act=check&usercode=" + value,
            dataType: "json",
            success: function (data) {
                $("#usercode-message").html("");
                if (data.result == "ok") {
                    $("#usercode-message").html("<span style='color:green;font-weight:bold;'>会员编号可用!</span>");
                } else {
                    $("#usercode-message").html("<span style='color:red;font-weight:bold;'>会员编号已被占用!</span>");
                }
            }
        });
    });

    //登录密码
    $("#password").validatebox({
        required: true,
        validType: "password",
        missingMessage: "登陆密码为必填项!"
    });

    $("#rpassword").validatebox({
        required: true,
        validType: "equal['#password']",
        missingMessage: "重复登陆密码为必填项!"
    });

    //二级密码
    $("#secondpassword").validatebox({
        required: true,
        validType: "password",
        missingMessage: "二级密码为必填项!"
    });

    $("#rsecondpassword").validatebox({
        required: true,
        validType: "equal['#secondpassword']",
        missingMessage: "重复二级密码为必填项!"
    });

    //三级密码
    $("#thirdpassword").validatebox({
        required: true,
        validType: "password",
        missingMessage: "三级密码为必填项!"
    });

    $("#rthirdpassword").validatebox({
        required: true,
        validType: "equal['#thirdpassword']",
        missingMessage: "重复三级密码为必填项!"
    });

    //注册金额
    $("#regmoney").numberbox({
        required: true,
        missingMessage: "注册金额为必填项!"
    });

    //服务中心
    $("#agentid").combogrid({
        required: true,
        missingMessage: "服务中心为必选项!",
        panelWidth: 260,
        loadMsg: "正在加载数据,请稍候...",
        idField: 'Number',
        textField: 'FullName',
        url: '../../controller.aspx?act=person',
        mode: "remote",
        pagination: true,
        columns: [[
					    { field: 'Number', title: 'Number', width: 60 },
					    { field: 'FullName', title: 'FullName', width: 100 }
				    ]]
    });

    //推荐会员
    $("#recommendid").combogrid({
        required: true,
        missingMessage: "推荐会员为必选项!",
        panelWidth: 260,
        loadMsg: "正在加载数据,请稍候...",
        idField: 'Number',
        textField: 'FullName',
        url: '../../controller.aspx?act=person',
        mode: "remote",
        pagination: true,
        columns: [[
					        { field: 'Number', title: 'Number', width: 60 },
					        { field: 'FullName', title: 'FullName', width: 100 }
				        ]]
    });

    //接点会员
    $("#parentid").combogrid({
        disabled: true,
        required: true,
        missingMessage: "节点会员为必选项!"
    });
    $("#parentid").combogrid("setValue", 1001);
    $("#parentid").combogrid("setText", "王明");

    //开户银行
    $("#bankid").combobox({
        required: true,
        missingMessage: "开户银行为必选项!",
        editable: false,
        url: "../../controller.aspx?act=bank",
        valueField: "id",
        textField: "text"
    });

    //银行所在省份
    $("#bankinprovinceid").combobox({
        required: true,
        missingMessage: "所在省份为必选项!",
        editable: false,
        url: "../../controller.aspx?act=province",
        valueField: "id",
        textField: "text"
    });

    //银行卡号
    $("#bankaccount").validatebox({
        required: true,
        validType: "bankcard",
        missingMessage: "银行卡号为必填项!"
    });

    //开户名称
    $("#bankaccountname").validatebox({
        required: true,
        missingMessage: "开户名称为必填项!"
    });

    //会员昵称
    $("#nickname").validatebox({
        required: true,
        missingMessage: "会员昵称为必填项!"
    });

    //会员姓名
    $("#truename").validatebox({
        required: true,
        missingMessage: "会员姓名为必填项!"
    });

    //身份证号
    $("#idcardno").validatebox({
        required: true,
        validType: "idcard",
        missingMessage: "身份证号为必填项!"
    });

    //联系电话
    $("#phone").validatebox({
        required: true,
        validType: "phone",
        missingMessage: "联系电话为必填项!"
    });

    /** 注册会员 **/
    $("#register-button").click(function () {
        $("#register-form").form("submit", {
            url: "../User/Register",
            onSubmit: function () {
                return $("#register-form").form("validate");
            },
            success: function (data) {
                $.messager.alert("注册成功", "恭喜您,帐号注册成功!", "input-info");
            }
        });
    });

    $("#register-form").stepy({
        nextLabel: "下一步",
        backLabel: "上一步",
        titleClick: true,
        description: true,
        legend: true,
        finishButton: false
    });

    /** 本地化 **/
    //$.linguaUpdateElements();

    /** Tooltip **/
    $("a").easyTooltip();

});
